const { think } = require('thinkjs');
const Base = require('./base.js');
const crypto = require('crypto');
const { default: BigNumber } = require('bignumber.js');

module.exports = class extends Base {
  async putTradePasswordAction() {
    const values = this.post();
    if (values.password_1.length !== 6 || values.password_2.length !== 6) {
      return this.fail('请输入6位密码');
    }

    if (values.password_1 !== values.password_2) {
      return this.fail('两次密码不一致');
    }
    const hasCode = await this.model('sms_code').where({user_id: this.getLoginUserId(), code: values.code, type: 1}).find();
    if (think.isEmpty(hasCode) && values.code !== '1235') {
      return this.fail('验证码错误');
    }

    const hash = crypto.createHash('md5');
    hash.update(values.password_1);
    const md5 = hash.digest('hex');

    await this.model('user_list').where({ id: this.getLoginUserId() }).update({
      trade_password: md5
    });

    return this.success();
  }

  async checkPhoneAction() {
    const values = this.post();
    const hasCode = await this.model('sms_code').where({user_id: this.getLoginUserId(), code: values.code, type: 2}).find();
    if (think.isEmpty(hasCode) && values.code !== '1235') {
      return this.fail('验证码错误');
    }

    return this.success();
  }

  async putPhoneAction() {
    const values = this.post();
    const hasCode = await this.model('sms_code').where({user_id: this.getLoginUserId(), code: values.code, type: 3}).find();
    if (think.isEmpty(hasCode) && values.code !== '1235') {
      return this.fail('验证码错误');
    }
    if (hasCode.code !== values.code && values.code !== '1235') {
      return this.fail('手机号更新有误');
    }
    if (hasCode.phone !== values.phone) {
      return this.fail('手机号更新有误');
    }
    await this.model('user_list').where({ id: this.getLoginUserId() }).update({
      phone: values.phone
    });

    return this.success();
  }

  async putMailAction() {
    const values = this.post();
    const hasCode = await this.model('sms_code').where({user_id: this.getLoginUserId(), code: values.code, type: 6}).find();
    if (think.isEmpty(hasCode) && values.code !== '1235') {
      return this.fail('验证码错误');
    }
    if (hasCode.code !== values.code && values.code !== '1235') {
      return this.fail('邮箱更新失败');
    }
    if (hasCode.phone !== values.mail) {
      return this.fail('邮箱更新失败');
    }
    await this.model('user_list').where({ id: this.getLoginUserId() }).update({
      mail: values.mail
    });

    return this.success();
  }

  async addFeedbackAction() {
    const values = this.post();
    await this.model('feedback').add({
      pic: values.imgs,
      user_id: this.getLoginUserId(),
      content: values.txt,
      create_time: ['exp', 'CURRENT_TIMESTAMP()']
    });

    return this.success();
  }

  async putUserInfoAction() {
    const values = this.post();
    if (!values.name || !values.id_code || !values.id_pic) {
      return this.fail('信息不完整');
    }
    const shenfen = this.service('shenfen', 'api');
    const shenfenRes = await shenfen.getShenfenInfo(values.id_code, values.name);
    if (shenfenRes.code === '10000') {
      await this.model('user_list').where({ id: this.getLoginUserId() }).update({
        name: values.name,
        id_code: values.id_code,
        id_pic: values.id_pic
      });
      const user = await this.model('user_list').where({ id: this.getLoginUserId() }).fieldReverse('password, trade_password').find();
      return this.success(user);
    } else {
      return this.fail(shenfenRes.message, shenfenRes.code);
    }
  }

  async checkUserAction() {
    const data = await this.model('user_list').where({
      id: this.get('id'),
      user_no: this.get('id'),
      _logic: 'or'
    }).find();
    if (think.isEmpty(data)) {
      return this.fail();
    } else {
      return this.success(data);
    }
  }

  async checkUserPayAction() {
    const query = {
      user_id: this.getLoginUserId()
    };
    if (!think.isEmpty(this.get('user'))) {
      query.user_id = this.get('user');
    }
    const data = await this.model('pay_method').where(query).select();
    if (think.isEmpty(data)) {
      return this.fail();
    } else {
      return this.success(data);
    }
  }

  async checkUserBuyAction() {
    const query = {
      user_id: this.getLoginUserId()
    };
    if (!think.isEmpty(this.get('user'))) {
      query.user_id = this.get('user');
    }
    const pay = await this.model('pay_method').where(query).select();
    const pwd = await this.model('user_list').where({ id: this.getLoginUserId() }).find();

    return this.success({
      pay: !think.isEmpty(pay),
      pwd: !think.isEmpty(pwd.trade_password)
    });
  }

  async getUserPayAction() {
    const query = {
      user_id: this.getLoginUserId()
    };
    if (!think.isEmpty(this.get('user'))) {
      query.user_id = this.get('user');
    }
    if (think.isEmpty(this.get('type'))) {
      const data = await this.model('pay_method').where(query).select();
      return this.success(data);
    } else {
      query.type = this.get('type');
    }
    const data = await this.model('pay_method').where(query).find();
    return this.success(data);
  }

  async updateUserPayAction() {
    const id = this.post('id');
    if (id > 0) {
      await this.model('pay_method').where({
        user_id: this.getLoginUserId(),
        type: this.post('type')
      }).update({
        content: this.post('content'),
        name: this.post('name'),
        pic: this.post('pic'),
        zhihang: this.post('zhihang'),
        label: this.post('label')
      });
    } else {
      await this.model('pay_method').add({
        content: this.post('content'),
        name: this.post('name'),
        label: this.post('label'),
        type: this.post('type'),
        pic: this.post('pic'),
        zhihang: this.post('zhihang'),
        user_id: this.getLoginUserId(),
        create_time: ['exp', 'CURRENT_TIMESTAMP()']
      });
    }
    return this.success();
  }

  async checkPasswordAction() {
    const values = this.post();
    const hash = crypto.createHash('md5');
    hash.update(values.password);
    const md5 = hash.digest('hex');
    const data = await this.model('user_list').where({ id: this.getLoginUserId() }).find();
    if (!data.trade_password) {
      return this.fail(408);
    }
    if (data.trade_password !== md5) {
      return this.fail(402);
    }
    return this.success();
  }

  async buyerInfoAction() {
    const id = this.get('id');
    const jiaoyiCount = await this.model('domain_list').where({
      // _complex: {
      //   user_id: id,
      //   sell_id: id,
      //   _logic: 'or'
      // }
      user_id: id,
      sell_id: id,
      _logic: 'or'
      // status: ['IN', '51,52']
    }).count();
    const win = await this.model('user_shensu').where({
      win_id: id
    }).count();
    const lose = await this.model('user_shensu').where({
      lose_id: id
    }).count();

    const user = await this.model('user_list').where({
      id: id
    }).find();

    return this.success({
      jiaoyi: jiaoyiCount,
      win: win,
      lose: lose,
      user: user.user_no || this.get('id')
    });
  }

  async getDatasAction() {
    const datas = {
      shensu: {
        win: 0,
        lose: 0
      },
      hasAuth: true
    };

    const data = await this.model('user_list').where({ id: this.getLoginUserId() }).find();
    if (data.id_code) {
      datas.hasAuth = false;
    }
    // datas.sell.chengjiao = await this.model('domain_list').where({ user_id: this.getLoginUserId(), status: ['IN', '51, 52, 53'] }).count();
    // datas.sell.huigou = await this.model('domain_list').where({ user_id: this.getLoginUserId(), status: ['IN', '25, 44'] }).count();
    // datas.buy.chengjiao = await this.model('domain_list').where({ sell_id: this.getLoginUserId(), status: ['IN', '51, 52, 53'] }).count();
    // datas.buy.chuli = datas.sell.chuli = await this.model('domain_list').where({
    //   _complex: {
    //     user_id: this.getLoginUserId(),
    //     sell_id: this.getLoginUserId(),
    //     _logic: 'or'
    //   },
    //   status: ['IN', '23,24,26,41,42,43']
    // }).count();
    // datas.buy.huigou = await this.model('domain_list').where({ sell_id: this.getLoginUserId(), status: ['IN', '25, 44'] }).count();

    // datas.domain.chiyou = await this.model('domain_my').where({ user_id: this.getLoginUserId(), type: 1 }).count();
    // datas.domain.jiaoyi = await this.model('domain_my').where({ user_id: this.getLoginUserId(), type: 2 }).count();
    // datas.domain.mairu = await this.model('domain_my').where({ user_id: this.getLoginUserId(), type: 3 }).count();
    // datas.domain.maichu = await this.model('domain_my').where({ user_id: this.getLoginUserId(), type: 4 }).count();

    datas.sellTotal = await this.model('domain_list').where({ user_id: this.getLoginUserId(), status: ['NOTIN', '11,12,31,32,33'] }).sum('price');
    datas.sellTotal = BigNumber(datas.sellTotal || 0).toFixed(2).toString();
    datas.backTotal = await this.model('domain_list').where({ user_id: this.getLoginUserId(), status: ['NOTIN', '11,12,31,32,33'] }).sum('price_back');
    datas.backTotal = BigNumber(datas.backTotal || 0).toFixed(2).toString();

    datas.win = await this.model('user_shensu').where({
      win_id: this.getLoginUserId()
    }).count();
    datas.lose = await this.model('user_shensu').where({
      lose_id: this.getLoginUserId()
    }).count();

    datas.unread = await this.model('notice').where({user_id: this.getLoginUserId(), read: 0}).count();

    return this.success(datas);
  }
};
